package com.stream;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class Demo01 {
	public static void main(String[] args) {
		//产生一个并行流
//		IntStream.range(1, 100).parallel().peek(Demo01::debug).count();
		/**
		 * 1:并行  .parallel()
		 * 2:串行 .sequeetial
		 * 多次调用并行，串行以最后一次为准
		 */
		/*
		IntStream.range(1, 100)
		.parallel().peek(Demo01::debug)
		.sequential().peek(Demo01::err)
		.count();
		*/
		//默认为CUP的个数
		//使用属性可以修改默认个数
		
		//使用自定义线程池，防止任务阻塞
		ForkJoinPool pool = new ForkJoinPool(20);
		pool.submit(
				() -> IntStream.range(1, 100)
				.parallel()
				.peek(Demo01::debug)
				.count());
		pool.shutdown();
		synchronized (pool) {
			try {
				pool.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public static void debug(int i){
		System.out.println(Thread.currentThread().getName()+"debug"+i);
		try {
			TimeUnit.SECONDS.sleep(3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	public static void err(int i){
		System.err.println("err"+i);
		try {
			TimeUnit.SECONDS.sleep(3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
